package com.hngy.xpq.android_practice.merchant_main.Comm;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hngy.xpq.android_practice.R;
import com.hngy.xpq.android_practice.assets.XpqIcoMoon;
import com.hngy.xpq.android_practice.assets.XpqIcoMoonUtils;
import com.hngy.xpq.android_practice.ui.home.User_Comm;

import java.text.SimpleDateFormat;
import java.util.Date;

import pl.droidsonroids.gif.GifImageView;

public class Comm_Make extends AppCompatActivity {

    Button xia, sha, begin;

    LinearLayout module;                    //控件创建
    GifImageView gifImageView;              //gif格式控件
    ProgressBar progressBar;                //进度条
    private int step = 0;                   //步骤控制
    private String begin_time = null;       //获取开始时间
    private String end_time = null;         //获取结束时间
    final int TIME = 100;                   //定义进度条的总时长
    final int TIME_GAS = 0X11;
    float MATH = 0;                         //进度条目前进度
    private int mTIME = 0;
    int times = 0;                          //推算烹饪时间
    String Imath = "正在烹饪";                //初始文字解释
    TextView textView,note,text,math;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comm__make);

        xia = findViewById(R.id.bin_xia);
        sha = findViewById(R.id.bin_sha);
        module = findViewById(R.id.line_module);
        gifImageView = findViewById(R.id.loading_img);
        progressBar = findViewById(R.id.progress);
        note = findViewById(R.id.text_note);
        text = findViewById(R.id.text);
        math = findViewById(R.id.math);

        //添加注意文字图片
        note.setText(XpqIcoMoon.ZY);
        XpqIcoMoonUtils.setIOC(note);

        SwitchMake();

        //上一步
        sha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (step < 0) {
                    step = 0;
                } else if (step > 0) {
                    step--;
                    SwitchMake();
                    if (step == 0) {
                        sha.setTextColor(0x84000000);
                    }
                }
            }
        });

        //下一步
        xia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                step++;
                SwitchMake();
                sha.setTextColor(0xFF000000);
            }
        });

    }

    //进度条的进行
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (mTIME < TIME) {
                mTIME ++;
                MATH ++;
                if (mTIME <= 60) {
                    //做法模块
                    if (mTIME < 10) {
                        text.setText("放入鸡块,直至鸡肉表面冒泡");
                    } else if (mTIME < 30) {
                        text.setText("放入辣椒,进行调味");
                    } else if (mTIME < 50) {
                        text.setText("放入青菜，小火慢炖");
                    }
                        Imath += ".";
                    if (Imath.equals("正在烹饪...")){
                        Imath = "正在烹饪";
                    }
                    math.setTextColor(0xFF67DD38);
                    progressBar.setProgressTintList(ColorStateList.valueOf(0xFF67DD38));
                } else if (mTIME <= 80) {
                    Imath = "进入成熟阶段,请按照烹饪要求熄火";
                    math.setTextColor(0xFFDB6235);
                    progressBar.setProgressTintList(ColorStateList.valueOf(0xFFDB6235));
                } else {
                    Imath = "启动紧急方案";
                    math.setTextColor(0xFFE11026);
                    progressBar.setProgressTintList(ColorStateList.valueOf(0xFFE11026));
                }
                math.setText(Imath);
                progressBar.setProgress(mTIME);
                handler.sendEmptyMessageDelayed(TIME_GAS,(times * 100));
            }
        }
    };

    private void SwitchMake() {
        switch (step) {
            case 0:
                //添加提醒
                text.setText("注意用刀安全!!");
                //测试:第一步.创建TextView控件，显示第一步切菜getBaseContext()
                gifImageView.setBackgroundResource(R.drawable.meisi005);
                textView = new TextView(this.getApplicationContext());
                textView.setLayoutParams(new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT));
                //测试:输入文本
                textView.setTextSize(20);
                textView.setText("食材准备:\n\n1、鸡肉   250g   处理形状:块状 \n2、辣椒   10g     处理形状:条状 \n3、青菜   100g   处理形状:片状 ");
                module.addView(textView);
                module.removeView(begin);
                break;
            case 1:
                xia.setText("下一步");
                //添加提醒
                text.setText("请注意离油锅远点，防止溅射!!");
                gifImageView.setBackgroundResource(R.drawable.meisi009);
                //测试:第二步,创建计时器，通过平均热量的思想，计算火候，推算成熟时间
                module.removeView(textView);
                module.removeView(progressBar);
                module.removeView(math);
                begin = new Button(this.getApplicationContext());
                begin.setText("开始");
                begin.setLayoutParams(new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT));
                module.addView(begin);
                begin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mTIME = 0;
                        if (begin.getText().equals("开始")) {
                            begin_time = ObtainTime();
                            System.out.println(begin_time);
                        } else {
                            end_time = ObtainTime();
                            System.out.println(end_time);
                            begin.setTextColor(0x84000000);
                            //获取平均热量计算烹饪时间
                            String[] begin_times = begin_time.split(":");
                            String[] end_times = end_time.split(":");
                            times = (Integer.valueOf(end_times[0]) - Integer.valueOf(begin_times[0])) * 60 + Integer.valueOf(end_times[1]) - Integer.valueOf(begin_times[1]);
                        }
                        begin.setText("结束");
                    }
                });
                break;
            case 2:
                gifImageView.setBackgroundResource(R.drawable.meisi001);
                //完成隐藏下一步
                xia.setText("完成");
                //测试:第三步，通过进度条模拟做菜流程
                module.removeView(begin);           //删除上一界面所留下的控件
                module.addView(progressBar);        //复原进度条
                module.addView(math);               //复原显示
                handler.sendEmptyMessage(TIME_GAS);
                begin.setText("");
                break;
            default:
                Intent intent = new Intent(Comm_Make.this, User_Comm.class);
                startActivity(intent);
                break;
        }
    }

    //获取时间函数
    private String ObtainTime() {
        //定义获取时间的格式
        SimpleDateFormat Sdf = new SimpleDateFormat("mm:ss");
        Date date = new Date(System.currentTimeMillis());
        return Sdf.format(date);
    }
}